package com.web;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration  //代表此类是一个spring的配置
@ComponentScan("com.web")    //扫描
@PropertySource("jdbc.properties")   //读取properties配置文件
public class SpringConfig {

    @Value("${driverClass}")
    private String driver;
    @Value("${jdbcUrl}")
    private String url;
    @Value("${user}")
    private String username;
    @Value("${password}")
    private String password;



    @Bean
    public DataSource getDs(){
        ComboPooledDataSource ds=new ComboPooledDataSource();
        try {
            ds.setDriverClass(driver);
            ds.setJdbcUrl(url);
            ds.setUser(username);
            ds.setPassword(password);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }

        return ds;
    }

    @Bean
    public DataSource getDs2(){
        ComboPooledDataSource ds=new ComboPooledDataSource();
        try {
            ds.setDriverClass(driver);
            ds.setJdbcUrl(url);
            ds.setUser(username);
            ds.setPassword(password);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }

        return ds;
    }
}